package app.netant.galgame.yuinya.net;

import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;
import cn.lily.com.db.sql.manager.DBOfflineManager;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

public class YuiNyaNetRoot {
	
	private static String homeUrl = "http://yui-nya.com/category/games/xz";
	
	private static String pageUrl = "http://yui-nya.com/category/games/xz/page/";
	
	public static void analyse(int maxIdx){
		
		System.out.println("整体处理开始........");

		resume();
		
		for(int i=1;i<=maxIdx;i++){
			
			String url = "";
			if(i == 1){
				url = homeUrl;
			}else{
				url = pageUrl + i;
			}
			
			System.out.println("第"+i+"页数据开始处理！");
			
			try {
				YuiNyaNetPage page = new YuiNyaNetPage(url);
				page.analyse();
			} catch (Exception e) {
				e.printStackTrace();
				getDBManager().executeUpdate("insert into ${schema}.LEO_YUINYA_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
										+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");					
				continue;
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public static DBManager getDBManager(){
		return DBOfflineManager.getDBManager();
	}
	
	/**
	 * 断点续传
	 */
	public static void resume(){
		
		System.out.println("断点续传处理开始........");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_YUINYA_GAL_INFO "
				+ "where GAME_HREF in "
				+ "(select EXCEP_URL from ${schema}.LEO_YUINYA_EXCEP)");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_YUINYA_EXCEP");
		
		System.out.println("断点续传处理结束........");
		
	}
	
	public static void error(){
		
		System.out.println("异常处理开始");
		
		getDBManager().executeUpdate("delete from ${schema}.LEO_YUINYA_GAL_INFO "
				+ "where GAME_HREF in "
				+ "(select EXCEP_URL from ${schema}.LEO_YUINYA_EXCEP)");
		
		DataTable table = getDBManager().executeQueryTable("select * from ${schema}.LEO_YUINYA_EXCEP");
		
		while(table.next()){
			
			String href = table.getString("EXCEP_URL");
			String level = table.getString("EXCEP_LEVEL");
			
			System.out.println("异常地址开始处理："+href);
			
			try {
				
				if("1".equals(level)){
					
					YuiNyaNetPage page = new YuiNyaNetPage(href);
					page.analyse();
					
				}
				
				else if("2".equals(level)){
					
					YuiNyaNetEntity entity = new YuiNyaNetEntity();
					entity.setGameHref(href);
					
					YuiNyaNetDetail detail = new YuiNyaNetDetail(href);
					detail.setEntity(entity);
					detail.analyse();
					
					entity.insert();
					
				}
				
				getDBManager().executeUpdate("delete from ${schema}.LEO_YUINYA_EXCEP where EXCEP_URL='"+href+"'");
				
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
			
		}
		
		System.out.println("异常处理结束");
		
	}
	
	public static void errorName(){
		DataTable table = getDBManager().executeQueryTable("select * from ${schema}.LEO_YUINYA_GAL_INFO");
		while(table.next()){
			String gameName = table.getString("GAME_NAME").trim();
			if(gameName.endsWith("版")){
				
				String id = table.getString("GAME_ID");
				
				/*int gameLength = gameName.length();
				int lastLength = gameName.lastIndexOf(' ');
				if(lastLength>0 && lastLength<gameLength-1){
					
					String newName = gameName.substring(0,lastLength);
					
					getDBManager().executeUpdate("update ${schema}.LEO_YUINYA_GAL_INFO set GAME_NAME='"+newName+"' where GAME_ID='"+id+"'");
				}*/
				
				System.out.println(id);
				
				
			}
			
			
		}
		
		
	}
	
	public static void main(String[] args) {
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		//analyse(129);
		
		error();
		
		//errorName();
		
	}
	
}
